﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinApp.Model;
using WinApp.Model.EF_Model;

namespace WinApp.View
{
    public partial class EfSqlLite : UIForm
    {
        public EfSqlLite()
        {
            InitializeComponent();
        }

        private void EfSqlLite_Load(object sender, EventArgs e)
        {
            QueryData();
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            // 获取当前选中的行（假设只允许单选）
            var selectedRow = uiDataGridView1.SelectedRows[0];
            if (selectedRow == null || selectedRow.Cells == null || selectedRow.Cells[0] == null || selectedRow.Cells[0].Value == null)
            {
                return;
            }
            var objId = selectedRow.Cells[0].Value;
            int.TryParse(objId.ToString(), out int Id);

            using (var db = new SqlLiteDbContext())
            {
                var currentRow = db.Courses.Find(Id);
                if (currentRow != null)
                {
                    db.Courses.Remove(currentRow);
                    db.SaveChanges();
                }
            }
            QueryData();
        }


        private void btnQuery_Click(object sender, EventArgs e)
        {
            QueryData();
        }

        public void QueryData()
        {
            using (var db = new SqlLiteDbContext())
            {
                var result = db.Courses.ToList();
                uiDataGridView1.DataSource = result;
            }
        }
    }
}
